<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row">
                <form class="layui-form" lay-filter="myform" id="myform">
                    <input type="reset" id="btReset" class="layui-hide" >
                    <div class="layui-form-item">
                        <label class="layui-form-label">题干</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" id="name"  autocomplete="off" class="layui-input">
                        </div>
                        <label class="layui-form-label">题型</label>
                        <div class="layui-input-inline">
                            <select name="questionType" id="questionType"></select>
                        </div>
                        <label class="layui-form-label">语言</label>
                        <div class="layui-input-inline">
                            <select name="lg" id="lg"></select>
                        </div>
                        <label class="layui-form-label">难度</label>
                        <div class="layui-input-inline">
                            <select name="difficulty" id="difficulty"></select>
                        </div>
                        <label class="layui-form-label">试题来源</label>
                        <div class="layui-input-inline">
                            <select name="source" id="source"></select>
                        </div>
                        <label class="layui-form-label">试题类型</label>
                        <div class="layui-input-inline">
                            <select name="contentType" id="contentType"></select>
                        </div>
                    </div>

                    <div class="layui-form-item" style="text-align: center">
                        <div class="layui-inline" style="margin-left: 5px;">
                            <div class="layui-btn-group">
                                <button type="button" class="layui-btn layui-btn-primary" data-type="query"><i class="layui-icon">&#xe615;</i>查询</button>
                                <button type="button" class="layui-btn layui-btn-primary" data-type="resetBtn"><i class="layui-icon">&#xe669;</i>重置</button>
                                <button type="button" class="layui-btn layui-btn-primary" data-type="queryRepeat"><i class="layui-icon layui-icon-search"></i>查重</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-row layui-col-space5">
                <table class="layui-hide"  id="main-table" lay-filter="main-table"></table>
            </div>
        </div>
    </div>
</div>
<button type="button" id="importWord" style="display: none">导入</button>
<script th:inline="none"></script>
<script>
    layui.use(['table','form','upload'], function(){
        var table = layui.table,
            form = layui.form,
            upload = layui.upload;
        createSelect("paper-lg","lg",form,'','请选择');
        createSelect("question-source","source",form,'','请选择');
        createSelect("question-difficulty","difficulty",form,'','请选择');
        createSelect("paper-type","contentType",form,'','请选择');
        createSelect("question-type","questionType",form,'','请选择');
        table.render({
            elem: '#main-table'
            ,height: (top.height-125)+'px'
            ,url: 'libQuestion/getPageList'
            ,title: '题目列表'
            ,toolbar: '#toolbar'
            ,limit:20
            ,page: true
            ,autoSort:false
            ,cols: [
                [
                    {type: 'checkbox'}
                    ,{field: 'code', title: "编号",width:90,sort:true}

                    ,{field: 'id', title: "题干",width:320,sort:true,templet:function(d){
                        return "<a class='colum-link' href='javascript:void(0)' onclick='edit(\""+d.id+"\",\"" + d.pianName + "\" , this)'>详情 </a>" + d.text2;
                    }}
                    ,{field: 'questionTypeTxt',title: "题型",width:100}
                    ,{field: 'pianName', title: "篇",width:180}
                    ,{field: 'chapterName', title: "章",width:180}
                    ,{field: 'jieName', title: "节",width:250}
                    ,{field: 'knowledgeName',title: "知识点",width:250}
                    ,{field: 'difficultyTxt',title: "难度",width:90}
                    ,{field: 'contentTypeTxt',title: "试题类型",width:120}
                    ,{field: 'paperTypeTxt',title: "试卷类型",width:120}
                    ,{field: 'lgTxt',title: "语言",width:90}
                    ,{field: 'stateTxt',title: "状态",width:90,sort:true}
                    ,{field: 'ignoreRepeatTxt',title: "可否查重",width:90}
                    ,{field: 'createTime', title: "登记时间",width:170,sort:true}
                    ,{field: 'lastUseDate',title: "组卷日期",width:120}
                ]
            ]
            ,done:function(){
                enableRowClick(table,form,'main-table',"main-table");
            }
        });
        table.on('sort(main-table)', function(obj){
            var field = obj.field;
            if(field=='createTime'){
                field = 'create_time'
            }
            table.reload('main-table', {
                initSort: obj, // 记录初始排序，如果不设的话，将无法标记表头的排序状态。
                where: { // 请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    field: field, // 排序字段
                    order: obj.type // 排序方式
                }
            });
        });
        table.on('edit(main-table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            axios.post('libQuestion/updateOrderno',obj.data).then(
                res=>{
                    layer.msg('保存成功',{icon:1});
                    table.reload('main-table');
                }
            )
        });
        //头工具栏事件
        table.on('toolbar(main-table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    top.layer.open({
                        id:"editwin",
                        type: 2,
                        title:'增加题目',
                        area:['1500px','880px'],
                        content:  'rooter?url=ksxt/lib/add-question1',
                        btn:['保存','重置','关闭'],
                        yes:function(index,layero){
                            var dataForm = top.layer.getChildFrame('form', index);
                            dataForm.find("#submitBtn").click();
                            return false
                        },btn2:function (index,layero){
                            var iframeWin = top.window[layero.find('iframe')[0]['name']];
                            iframeWin.revalue();
                            return false;
                        }
                    });
                    break;
                case 'ignore':
                    var data = checkStatus.data;
                    if(data.length>0){
                        var values = {};
                        var ids=[];
                        $.each(data,function(i,v){
                            ids.push(v.id);
                        })
                        values['ids'] = ids.join(",");
                        layer.confirm('确定忽略查重吗?', {icon: 3, title:'提示'}, function(index){
                            axios.post("libQuestion/ignore",values).then(res=>{
                                if (res.data.code == 1) {
                                    layer.msg(res.msg);
                                } else {
                                    layer.close(index);
                                    table.reload('main-table');
                                    layer.msg("设置忽略查重成功。");
                                }
                            })
                        });
                    }else{
                        layer.msg("请选择操作数据!")
                    }
                    break;
                case 'delete':
                    var data = checkStatus.data;
                    if(data.length>0){
                        var values = {};
                        var ids=[];
                        $.each(data,function(i,v){
                            ids.push(v.id);
                        })
                        values['ids'] = ids.join(",");
                        layer.confirm('确定要删除当前行吗?', {icon: 3, title:'提示'}, function(index){
                            axios.post("libQuestion/delete",values).then(res=>{
                                if (res.data.code == 1) {
                                    layer.msg(res.msg);
                                } else {
                                    layer.close(index);
                                    table.reload('main-table');
                                    layer.msg("删除成功。");
                                }
                            })
                        });
                    }else{
                        layer.msg("请选择操作数据!")
                    }
                    break;
                case 'edit':
                    var data2 = checkStatus.data;
                    if(data2.length>0){
                        edit(data2[0].id);
                    }else{
                        layer.msg("请选择操作数据！")
                    }
                    break;
                case 'emptyLastUseDate':
                    axios.post('lib/emptyLastUseDate').then(
                        res=>{
                            layer.msg('操作成功',{icon:1});
                            table.reload('main-table');
                        }
                    )
                    break;
                case 'queryRepeat':
                    table.reload("main-table",{url:'libQuestion/querySimilarList'})
                    break;
                case 'importWordBtn':
                    $("#importWord").click();
                    break;
            };
        });
        var $ = layui.$, active = {
            query: function(){

                table.reload("main-table",{where:{name:$("#name").val(),difficulty:$("#difficulty").val()
                        ,lg:$("#lg").val(),questionType:$("#questionType").val(),contentType:$("#contentType").val()
                        ,source:$("#source").val()}})
            },
            resetBtn:function () {
                $("#btReset").click();

            },queryRepeat:function (){
                table.reload("main-table",{url:'libQuestion/querySimilarList'})
            }
        };
        window.loadGrid = function(tableName){
            table.reload(tableName);
        }
        // window.queryQuestion=function (id,name){
        //     localStorage.setItem("libid",id);
        //     top.$("#29").click();
        // }
        $('.layui-form .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        var url = 'libQuestion/importWord';
        upload.render({
            elem: '#importWord'
            ,url: url
            ,exts:"docx"
            ,before: function(obj){
                this.data.libId = libId
            },done: function(res, index, upload) {
                layer.msg(res.msg)
            },progress: function(n){

            },error: function(index, upload){

            }
        });
    });
    function edit(id){
        top.layer.open({
            id:"editwin",
            type: 2,
            title:'修改题目',
            area:['1500px','880px'],
            content:  'rooter?url=ksxt/lib/add-question1&delFlag=1&id='+id,
            btn:['保存','关闭'],
            yes:function(index,layero){
                var dataForm = top.layer.getChildFrame('form', index);
                dataForm.find("#submitBtn").click();
                return false
            }
        });
    }

</script>
<script type="text/html" id="toolbar">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-addition"></i>增加</button>
        <button class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</button>
<!--        <button class="layui-btn layui-btn-sm" lay-event="queryRepeat"><i class="layui-icon layui-icon-search"></i>查重</button>-->
        <button class="layui-btn layui-btn-sm" lay-event="ignore"><i class="layui-icon">&#xe6b2;</i>忽略查重</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="emptyLastUseDate"><i class="layui-icon layui-icon-delete"></i>置空组卷日期</button>
    </div>
</script>
</body>
</html>